package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseType;
import cn.itsource.ymcc.mapper.CourseTypeMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.ICourseTypeService;
import cn.itsource.ymcc.vo.CourseTypeCrumbsVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import sun.reflect.generics.tree.Tree;
import sun.reflect.generics.tree.TypeTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author zongcj
 * @since 2023-09-14
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public static final String COURSE_TYPE_TREE = "CourseTypeTree";

    @Override
    @Cacheable(cacheNames = "typeTree",key = "'course'")
    public JsonResult getCourseTypeTree() {
//        if(redisTemplate.hasKey(COURSE_TYPE_TREE)){
//            return JsonResult.success().setData(redisTemplate.opsForValue().get(COURSE_TYPE_TREE));
//        }
        //我们使用for  map的方式
        List<CourseType> courseTypes = courseTypeMapper.selectList(null);
        Map<Long, CourseType> map = courseTypes.stream()
                .collect(Collectors.toMap(CourseType::getId, Function.identity(),(k1,k2)->k1));
        List<CourseType> one = new ArrayList<>();
        for (CourseType courseType : courseTypes) {
            if(courseType.getPid().equals(RConst.Num.BASE_LONG_0)){
                one.add(courseType);
            }else {
                CourseType courseType1 = map.get(courseType.getPid());
                courseType1.getChildren().add(courseType);
            }
        }
//        redisTemplate.opsForValue().set(COURSE_TYPE_TREE,one);
        return JsonResult.success().setData(one);
    }

    @Override
    public JsonResult getLoadCrumbs(Long typeId) {
        List<CourseTypeCrumbsVo> list = new ArrayList<>();
        //怎么去找层级
        CourseType courseType = baseMapper.selectById(typeId);
        //1037.1038.1041
        String[] split = courseType.getPath().split("\\.");
        for (String s : split) {
            CourseTypeCrumbsVo courseTypeCrumbsVo = new CourseTypeCrumbsVo();
            //找自己和兄弟
            CourseType my = baseMapper.selectById(Long.valueOf(s));
            Long pid = my.getPid();
            List<CourseType> courseTypes = baseMapper.selectList(new LambdaQueryWrapper<CourseType>().eq(CourseType::getPid, pid));
            courseTypeCrumbsVo.setCurrent(my);
            courseTypeCrumbsVo.setBrothers(courseTypes);
            list.add(courseTypeCrumbsVo);
        }
        return JsonResult.success().setData(list);
    }

    @Override
    public boolean save(CourseType entity) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return false;
    }

    @Override
    public boolean update(Wrapper<CourseType> updateWrapper) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return false;
    }

    @Override
    @CacheEvict(cacheNames="books",key = "'course'")
    public boolean remove(Wrapper<CourseType> queryWrapper) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return false;
    }
}
